package com.example.backend.service;

import com.example.backend.entity.*;
import com.example.backend.entity.vo.Ddmx;
import com.example.backend.entity.vo.ShowCaiPin;
import com.example.backend.entity.vo.ShowYhDd;
import com.example.backend.entity.vo.Wdia;
import com.example.backend.mapper.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class WorkService {
    @Autowired
    ShangJiaMapper shangJiaMapper;
    @Autowired
    DingDanMapper dingDanMapper;
    @Autowired
    ShenFenMapper shenFenMapper;
    @Autowired
    PeiSongMapper peiSongMapper;
    @Autowired
    XiangQingMapper xiangQingMapper;
    @Autowired
    CaiPinMapper caiPinMapper;
    @Autowired
    PingJiaMapper pingJiaMapper;

    /**详细数据*/
    public Wdia query(Integer yhid) {
        ShangJia shangJia = shangJiaMapper.queryShangJiaByYhid(yhid);
        List<DingDan> dingDans = dingDanMapper.queryDingDanBySjid(shangJia.getSjid());//查询已完成支付的订单
        List<DingDan> dds = dingDanMapper.queryDingDanBySjidAndDate(shangJia.getSjid());//查询当日的所有订单
        List<PingJia> pingJiaList = pingJiaMapper.querypj(shangJia.getSjid());
        double total = 0.0;//今日营业额
        int count = 0;//今日完成订单数
        int ddwcl;//订单完成率
        int xzpl = pingJiaList.size();//今日新增评论
        int yhsl = dingDanMapper.countDistinctUsersBySjid(shangJia.getSjid()) ;//用户数量
        double pjkdj;//平均客单价
        for(DingDan dingDan : dingDans) {
            total += Double.parseDouble(String.valueOf(dingDan.getTPrice()));
            count++;
        }
        if(count == 0) {
            ddwcl = 0;
            pjkdj = 0;
        } else {
            ddwcl = count / dds.size() * 100;
            pjkdj = total / yhsl;
        }

        return new Wdia(total,count,ddwcl,pjkdj,xzpl);
    }


    /**订单详情表*/
    public ShowYhDd queryDdxqb(Integer yhid, String date) {
        ShowYhDd showYhDd = new ShowYhDd();
        DingDan dingDan = dingDanMapper.queryDingDanByYhidAndDdsj(yhid,date);
        ShangJia shangJia = shangJiaMapper.selectById(dingDan.getSjid());
        ShenFen shenFen = shenFenMapper.selectById(dingDan.getYhid());
//        PeiSong peiSong = peiSongMapper.queryPeiSongByDdid(dingDan.getDdid());

        List<XiangQing> xiangQingList = xiangQingMapper.queryXiangQingByddid(dingDan.getDdid());
        showYhDd.setDdid(dingDan.getDdid());
        showYhDd.setSjid(dingDan.getSjid());
        showYhDd.setName(shangJia.getName());
        showYhDd.setDdzt(dingDan.getDdzt());
        showYhDd.setDdsj(dingDan.getDdsj());
        showYhDd.setYhid(dingDan.getYhid());
        showYhDd.setYhnc(shenFen.getYhnc());
        showYhDd.setPhone(shenFen.getPhone());
//        showYhDd.setPsjs(peiSong.getPsjs());
        showYhDd.setDddz(dingDan.getDddz());
        showYhDd.setDdbz(dingDan.getDdbz());

        List<ShowCaiPin> showCaiPinList = new ArrayList<>();
        for (XiangQing xiangQing : xiangQingList) {
            Integer cpid = xiangQing.getCpid();
            CaiPin caiPin = caiPinMapper.selectById(cpid);
            String cpmc = caiPin.getCpmc();
            String cptp = caiPin.getCptp();
            Double cpdj = Double.valueOf(caiPin.getCpjg());
            Integer cpsl = xiangQing.getCpsl();
            ShowCaiPin showCaiPin = new ShowCaiPin(cpid,cpmc,cptp,cpdj,cpsl);
            showCaiPinList.add(showCaiPin);
        }
        showYhDd.setCaiPin(showCaiPinList);
        System.out.println(showYhDd.getCaiPin());
        showYhDd.setPsf(Double.valueOf(shangJia.getPsf()));
        showYhDd.setDbf(Double.valueOf(shangJia.getDbf()));
        double ddTotal = 0.0;
        for (ShowCaiPin showCaiPin : showYhDd.getCaiPin()) {
            ddTotal += showCaiPin.getCpjg() * showCaiPin.getCpsl();
        }
        ddTotal += showYhDd.getPsf();
        ddTotal += showYhDd.getDbf();
        showYhDd.setTotal(ddTotal);
        return showYhDd;
    }

    /**订单明细*/
    public Ddmx queryDdmx(Integer sjid) {
        List<DingDan> yzfDD = dingDanMapper.queryDingDanBySjid(sjid);  //查询已支付的订单
        List<DingDan> wzfDD = dingDanMapper.queryDingDanBySjidAndNotPay(sjid);  //查询未支付的订单
        List<DingDan> allDD = dingDanMapper.queryDingDanBySjidAndDate(sjid);    //所有订单

        int yzf = yzfDD.size();     //已支付数量
        int wzf = wzfDD.size();     //未支付数量
        int count = allDD.size();   //所有订单数量

        List<PeiSong> wpsDD = peiSongMapper.queryWPeiSongByYhid(sjid);     //查询未配送的订单
        List<PeiSong> pszDD = peiSongMapper.queryPeiSongZByYhid(sjid);     //查询配送中的订单
        List<PeiSong> ysdDD = peiSongMapper.queryYiSongDaByYhid(sjid);     //查询已送达的订单

        int wps = wpsDD.size();     //未配送数量
        int psz = pszDD.size();     //配送中数量
        int ysd = ysdDD.size();     //已送达数量

        return new Ddmx(wps,psz,ysd,wzf,yzf,count);
    }
}
